(function(){
	$('#search_result').live('pageinit', function(event){
		$('.poll-opt').parent().click(vote_option);
	});
	var lat, lng						// get latitude and longitude of current location
	function success(position){
		lat = position.coords.latitude;
		lng = position.coords.longitude;
	};
	if (navigator.geolocation) {
  		navigator.geolocation.getCurrentPosition(success);
	} else {
  	error('not supported');
	}
	function vote_option()				// by using ajax, get a user status, and decide the click event : vote / login / mid-result show
	{
		var opt = $(this).find(".poll-opt").val().split(";");
		var poll_id = opt[0];
		var mid_show = opt[1];
		$.ajax({										// ajax
	  		type: "post",
	  		url: CI_ROOT + "ajax/can_user_vote",
	  		data: {
	  			poll_id : poll_id,
	  			lat : lat,
	  			lng : lng	  			
	  		},
	  		success : function(res){					
	  			if(res.status == 'ok'){
	  				if(mid_show == 1){
	  					$.mobile.activePage.simpledialog({		// simple dialog is popped up to select vote or mid-result show
									'mode' : 'blank',
									'prompt': false,
									'forceInput': false,
									'cleanOnClose': true,
									'useModal': true,
									'fullHTML' : 
										"<a rel='close' data-role='button' href=" + CI_ROOT + "poll/vote/"+poll_id 
										+ " id='simpleclose'>설문하기</a> <a rel='close' data-role='button' href=" + CI_ROOT + "poll/result/"+poll_id
										 + " id='simpleclose'>중간결과보기</a>"
						});
	  				} else $.mobile.changePage( "vote/"+poll_id, { transition: "slideup"} );
	  			}else if(res.message){
	  				$.mobile.showNotification(res.message);    // error message is printed out if user is not allowed to vote
	  			}		
	  		},
	  		error : function(res){
	  			alert("an error occured");
	  		}
		});
	}
})();
